package com.whq.knowverywell.business.http;

import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Context;

import com.whq.knowverywell.business.LoginUtil;
import com.whq.knowverywell.business.http.model.BaseSalesReturn;
import com.whq.knowverywell.business.model.BaseSalesRecord;
import com.whq.knowverywell.util.LogUtil;
import com.whq.knowverywell.util.NetworkUtil;
import com.whq.knowverywell.util.StringUtil;
import com.whq.knowverywell.util.constant.CfgConstant;
import com.whq.knowverywell.util.httpclient.HttpClientUtil;
import com.whq.knowverywell.util.httpclient.HttpResult;

/**
 * 获取品类销售数据
 * @author wanghuaiqiang
 *
 */
public class GetPropertySalesDataUtil {
	private static final String TAG = "GetPropertySalesDataUtil";
	
	public static BaseSalesReturn<BaseSalesRecord> getTodayDataList(Context context, String orderBy, int pageNumber, int recordCount) {
		return getList(context, "today", orderBy, pageNumber, recordCount);
	}
	
	public static BaseSalesReturn<BaseSalesRecord> getYesterdayDataList(Context context, String orderBy, int pageNumber, int recordCount) {
		return getList(context, "yesterday", orderBy, pageNumber, recordCount);
	}
	
	public static BaseSalesReturn<BaseSalesRecord> getCurrentMonthDataList(Context context, String orderBy, int pageNumber, int recordCount) {
		return getList(context, "currentMonth", orderBy, pageNumber, recordCount);
	}
	
	public static BaseSalesReturn<BaseSalesRecord> getCurrentYearDataList(Context context, String orderBy, int pageNumber, int recordCount) {
		return getList(context, "currentYear", orderBy, pageNumber, recordCount);
	}
	
	private static BaseSalesReturn<BaseSalesRecord> getList(Context context, String dataType, String orderBy, int pageNumber, int recordCount) {
		BaseSalesReturn<BaseSalesRecord> retObj = new BaseSalesReturn<BaseSalesRecord>();
		List<BaseSalesRecord> retList = retObj.getRecordList();
		
		if (context == null) {
			return retObj;
		}
		if (!NetworkUtil.isNetworkAvailable(context)) {
			return retObj;
		}
		
		String token = LoginUtil.getLoginToken(context);
		if (StringUtil.isBlank(token)) {
			return retObj;
		}
		
		// 获取guid
		String path = CfgConstant.SERVER_CONTEXT + "/property/propertySalesList";
		StringBuilder sb = new StringBuilder();
		sb.append("token=" + token);
		sb.append("&dataType=" + dataType);
		sb.append("&orderBy=" + orderBy);
		sb.append("&pageNumber=" + pageNumber);
		sb.append("&recordCount=" + recordCount);
		sb.append("&version=" + CfgConstant.SERVER_API_VERSION);
		sb.append("&source=" + CfgConstant.SERVER_API_SOURCE);
		
		String req = sb.toString();
		HttpResult httpResult = HttpClientUtil.callServer(context, CfgConstant.SERVER_HOST, false, path, req, "UTF-8");
		if ( (httpResult == null) 
				|| (!httpResult.checkCode()) 
				) {
			return retObj;
		}
		
		String responseStr = httpResult.getResp();
		if (StringUtil.isBlank(responseStr)) {
			return retObj;
		}
		
		try {
			JSONObject recordJson = new JSONObject(responseStr);
			int resultCode = recordJson.optInt("resultCode", -1);
			if (resultCode != 0) {
				ChkResponseUtil.chkLogoutErrorCode(context, resultCode);
				return retObj;
			}
			
			if (recordJson.isNull("data")) {
				return retObj;
			}
			
			JSONObject dataJson = recordJson.getJSONObject("data");
			
			retObj.setTotalSalesAmount(dataJson.optDouble("TotalSalesAmount", 0));
			retObj.setTotalSalesVolume(dataJson.optLong("TotalSalesVolume", 0l));
			
			JSONArray listArray = dataJson.getJSONArray("List");
			if (listArray == null) {
				return retObj;
			}
			int arrayLength = listArray.length();
			for (int i = 0; i < arrayLength; i++) {
				JSONObject itemJson = listArray.getJSONObject(i);
				
				BaseSalesRecord oneRecord = new BaseSalesRecord();
				oneRecord.setCode(itemJson.optString("Code", ""));
				oneRecord.setName(itemJson.optString("Name", ""));
				oneRecord.setSalesAmount(itemJson.optDouble("SalesAmount", 0));
				oneRecord.setSalesVolume(itemJson.optLong("SalesVolume", 0));
				
				retList.add(oneRecord);
				retObj.setEndCode(oneRecord.getCode());
			}
		} catch (Exception e) {
			LogUtil.e(TAG, "解析json串出错：", e);
		}
		return retObj;
	}
	
}
